package jpave.manager;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

import util.ui.UserPrompt;

public class GenRepsPanel extends JPanel {
	private static final long serialVersionUID = -1652821001608191481L;

	private static final String [] COL_NAMES = { "SeqID", "File Lib name", "Real Lib name" };
	private static final int TABLE_HEIGHT = 300;

	public GenRepsPanel(ManagerFrame parentFrame) {
		theParentFrame = parentFrame;
		theModel = new TheTableModel();
		theTable = new JTable();
		theTable.setModel(theModel);
		sPane = new JScrollPane(theTable);
		sPane.getViewport().setBackground(ManagerFrame.BGCOLOR);
		Dimension d = sPane.getPreferredSize();
		d.height = TABLE_HEIGHT;
		sPane.setPreferredSize(d);
		sPane.setMaximumSize(d);
		sPane.setMinimumSize(d);
		
		btnKeep = new JButton("Keep");
		btnKeep.setBackground(ManagerFrame.BGCOLOR);
		btnKeep.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				theParentFrame.generateReps();
			}
		});
		
		btnDiscard = new JButton("Discard");
		btnDiscard.setBackground(ManagerFrame.BGCOLOR);
		btnDiscard.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				stopEditing();
				setVisible(false);
				theParentFrame.setMainPanelVisible(true);
			}
		});
		
		btnHelp = new JButton("Help");
		btnHelp.setBackground(ManagerFrame.HELPCOLOR);
		btnHelp.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				UserPrompt.displayHTMLResourceHelp(theParentFrame, "Generate Reps", "html/runSingleTCW/GenRepsPanel.html");
			}
		});
		
		JPanel buttonPanel = ManagerFrame.getPanel(true);
		buttonPanel.add(btnKeep);
		buttonPanel.add(Box.createHorizontalStrut(10));
		buttonPanel.add(btnDiscard);
		buttonPanel.add(Box.createHorizontalStrut(10));
		buttonPanel.add(btnHelp);

		buttonPanel.setMaximumSize(buttonPanel.getPreferredSize());
		buttonPanel.setMinimumSize(buttonPanel.getPreferredSize());
		buttonPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
		
		JPanel tablePanel = ManagerFrame.getPanel(true);
		tablePanel.add(sPane);
		
		JPanel mainPanel = ManagerFrame.getPanel(false);
		mainPanel.add(tablePanel);
		mainPanel.add(Box.createVerticalStrut(20));
		JPanel temp = ManagerFrame.getPanel(true);
		temp.add(Box.createHorizontalGlue());
		temp.add(buttonPanel);
		temp.add(Box.createHorizontalGlue());
		mainPanel.add(temp);
		mainPanel.setBorder(BorderFactory.createEmptyBorder(30, 30, 30, 30));
		mainPanel.setMaximumSize(mainPanel.getPreferredSize());
		mainPanel.setMinimumSize(mainPanel.getPreferredSize());
		mainPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
		
		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		setBackground(ManagerFrame.BGCOLOR);
		add(mainPanel);
		
		setVisible(false);
	}
	
	public void stopEditing() {
		if(theTable.isEditing())
			theTable.getCellEditor().stopCellEditing();
	}
	
	public String [] getAllTransLib() {
		String [] retVal = new String[theModel.getRowCount()];
		
		for(int x=0; x<retVal.length; x++) 
			retVal[x] = theModel.getTransLibAt(x);
		
		return retVal;
	}

	public String [] getAllRepLib() {
		String [] retVal = new String[theModel.getRowCount()];
		
		for(int x=0; x<retVal.length; x++) 
			retVal[x] = theModel.getRepidAt(x);
		
		return retVal;
	}

	public String [] getAllLibid() {
		String [] retVal = new String[theModel.getRowCount()];
		
		for(int x=0; x<retVal.length; x++) 
			retVal[x] = theModel.getLibidAt(x);
		
		return retVal;
	}

	public String [] getAllPrefix() {
		String [] retVal = new String[theModel.getRowCount()];
		
		for(int x=0; x<retVal.length; x++) 
			retVal[x] = theModel.getPrefixAt(x);
		
		return retVal;
	}

	public void setRepList(Vector<String> reps, Vector<String> prefix, Vector<String> libs, Vector<String> transLib) {
		theModel.clearAllData();
		
		for(int x=0; x<reps.size(); x++) {
			theModel.addRow(reps.get(x), prefix.get(x), libs.get(x), transLib.get(x));
		}
		
		theModel.fireTableDataChanged();
	}
	
	private JTable theTable = null;
	private TheTableModel theModel = null;
	private JScrollPane sPane = null;
	private JButton btnKeep = null;
	private JButton btnDiscard = null;
	private JButton btnHelp = null;
	private ManagerFrame theParentFrame = null;

	private class TheTableModel extends AbstractTableModel {
		private static final long serialVersionUID = -473598297867947687L;

		public TheTableModel() { theRows = new TableRow[0]; }
		
		public void addRow(String fileLibid, String prefix, String realLibid, String transLib) {
			TableRow [] temp = theRows;
			
			theRows = new TableRow[temp.length + 1];
			for(int x=0; x<temp.length; x++)
				theRows[x] = temp[x];
			theRows[theRows.length - 1] = new TableRow(fileLibid, prefix, realLibid, transLib);
		}
		
		public Object getValueAt(int row, int column) {
			if(column == 0)
				return theRows[row].strTransLib;
			if(column == 1)
				return theRows[row].strFileLibid;
			return theRows[row].strRealLibid;
		}

		public void setValueAt(Object val, int row, int column) {
			if(column == 0)
				theRows[row].strTransLib = (String)val;
			else if(column == 1)
				theRows[row].strFileLibid = (String)val;
			else
				theRows[row].strRealLibid = (String)val;
		}
		
		public String getLibidAt(int row) { return theRows[row].strRealLibid; }
		public String getRepidAt(int row) { return theRows[row].strFileLibid; }
		public String getPrefixAt(int row) { return theRows[row].strPrefix; }
		public String getTransLibAt(int row) { return theRows[row].strTransLib; }
		
		public void clearAllData() { theRows = new TableRow[0]; }
		public int getRowCount() { return theRows.length; }
		public Class<?> getColumnClass(int column) { return String.class; }
		public String getColumnName(int columnIndex) { return COL_NAMES[columnIndex]; }
		public boolean isCellEditable (int row, int column) { return column == 2; }
		public int getColumnCount() { return COL_NAMES.length; }
		
		private class TableRow {
			public TableRow(String fileLibid, String prefix, String realLibid, String transLib) {
				strFileLibid = fileLibid;
				strRealLibid = realLibid;
				strPrefix = prefix;
				strTransLib = transLib;
			}

			private String strFileLibid = "";
			private String strRealLibid = "";
			private String strPrefix = "";
			private String strTransLib = "";
		}
		
		private TableRow [] theRows = null;
	}
}
